package com.ssy.lingxi.purchase.controller.purchase;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.request.ImportBatchListRequest;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.purchase.api.dto.request.*;
import com.ssy.lingxi.purchase.api.dto.request.bidding.PurchaseMakeList;
import com.ssy.lingxi.purchase.api.dto.request.purchase.*;
import com.ssy.lingxi.purchase.api.dto.response.BiddingExternalWorkStateEnumResponse;
import com.ssy.lingxi.purchase.api.dto.response.bidding.PurchaseMakeListRequest;
import com.ssy.lingxi.purchase.api.dto.response.purchase.*;
import com.ssy.lingxi.purchase.entity.ImportBatchNo;
import com.ssy.lingxi.purchase.entity.purchaseinquiry.ExternalPurchaseInquiryLog;
import com.ssy.lingxi.purchase.entity.purchaseinquiry.InteriorPurchaseInquiryLog;
import com.ssy.lingxi.purchase.service.PurchaseInquiryService;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
* @menu 采购能力-采购询价
* @author wrc
* @date 2021/1/9
*/
@RestController
@RequestMapping("/purchase/purchase/inquiry")
public class PurchaseInquiryController extends BaseController {

    @Resource
    PurchaseInquiryService purchaseInquiryService;

    /**
    *采购询价-采购需求单查询
    */
    @GetMapping("/list")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseInquiryList(HttpServletRequest httpServletRequest, PurchaseInquiryListRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryList(sysUser,request);
    }

    /**
     *采购询价-采购需求单详情
     */
    @GetMapping("/details")
    public Wrapper<PurchaseInquiryDetailsResponse> purchaseInquiryDetails(BaseDetailsRequest request, HttpServletRequest httpServletRequest ) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryDetails(request,sysUser);
    }

    /**
     *采购询价-采购需求单详情
     */
    @GetMapping("/home/details")
    public Wrapper<PurchaseInquiryDetailsResponse> details(BaseDetailsRequest request) {
        return purchaseInquiryService.details(request, isLogin() ? getSysUser() : null);
    }

    /**
     *采购询价-采购需求单添加
     */
    @PostMapping("/add")
    public Wrapper purchaseInquiryAdd(@Validated @RequestBody PurchaseInquiryAddRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryAdd(request,sysUser);
    }

    /**
     *采购询价-采购需求单修改
     */
    @PostMapping("/update")
    public Wrapper purchaseInquiryUpdate(@Validated @RequestBody PurchaseInquiryUpdateRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryUpdate(request,sysUser);
    }

    /**
     *采购询价-采购需求单删除
     */
    @PostMapping("/delete")
    public Wrapper purchaseInquiryDelete(@Validated @RequestBody BaseRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryDelete(request,sysUser);
    }

    /**
     *采购询价-采购需求单批量删除
     */
    @PostMapping("/delete/batch")
    public Wrapper purchaseInquiryDeleteBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryDeleteBatch(request,sysUser);
    }

    /**
     *采购询价-待新增采购需求单
     */
    @GetMapping("/add/list")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseInquiryAddList(PurchaseInquiryListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryAddList(request,sysUser);
    }

    /**
     *采购询价-待新增商城采购需求单
     */
    @GetMapping("/shop/add/list")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseShopInquiryAddList(PurchaseInquiryListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseShopInquiryAddList(request,sysUser);
    }

    /**
     *采购询价-提交审核采购需求单
     */
    @PostMapping("/submit")
    public Wrapper purchaseInquiryExamine(@Validated @RequestBody BaseRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamine(request,sysUser);
    }

    /**
     *采购询价-提交审核采购需求单批量
     */
    @PostMapping("/submit/batch")
    public Wrapper purchaseInquiryExamineBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamineBatch(request,sysUser);
    }

    /**
     *采购询价-待审核采购需求单(一级)
     */
    @GetMapping("/stay/examine/list1")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseInquiryStayExamineList1(PurchaseInquiryListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryStayExamineList1(request,sysUser);
    }

    /**
     *采购询价-审核采购需求单(一级)
     */
    @PostMapping("/examine1")
    public Wrapper purchaseInquiryExamine1(@Validated @RequestBody BaseExamineRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamine1(request,sysUser);
    }

    /**
     *采购询价-审核采购需求单(一级)批量
     */
    @PostMapping("/examine1/batch")
    public Wrapper purchaseInquiryExamine1Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamine1Batch(request,sysUser);
    }

    /**
     *采购询价-待审核采购需求单(二级)
     */
    @GetMapping("/stay/examine/list2")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseInquiryStayExamineList2(PurchaseInquiryListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryStayExamineList2(request,sysUser);
    }

    /**
     *采购询价-审核采购需求单(二级)
     */
    @PostMapping("/examine2")
    public Wrapper purchaseInquiryExamine2(@Validated @RequestBody BaseExamineRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamine2(request,sysUser);
    }

    /**
     *采购询价-审核采购需求单(二级)批量
     */
    @PostMapping("/examine2/batch")
    public Wrapper purchaseInquiryExamine2Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryExamine2Batch(request,sysUser);
    }

    /**
     *采购询价-待提交采购需求单
     */
    @GetMapping("/stay/commit/list")
    public Wrapper<PageData<PurchaseInquiryListResponse>> purchaseInquiryStayCommitList(PurchaseInquiryListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryStayCommitList(request,sysUser);
    }

    /**
     *采购询价-提交采购需求单
     */
    @PostMapping("/commit")
    public Wrapper purchaseInquiryCommit(@Validated @RequestBody BaseExamineRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryCommit(request,sysUser);
    }

    /**
     *采购询价-提交采购需求单批量
     */
    @PostMapping("/commit/batch")
    public Wrapper purchaseInquiryCommitBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryCommitBatch(request,sysUser);
    }

    /**
     * 采购询价-需求对接-初始化系统匹配会员信息列表初始化
     * @author wrc
     * @date 2020/8/3
     */
    @PostMapping("/system/matching/member/initialize/list")
    public Wrapper<PageData<DemandMemberResponse>> systemMatchingMemberKInitializeList(@Validated  @RequestBody MatchingMemberKInitializeListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.systemMatchingMemberKInitializeList(request,sysUser);
    }

    /**
     * 采购询价-需求对接-查询系统匹配会员信息列表-分页查询
     * @author wrc
     * @date 2020/8/3
     */
    @GetMapping("/system/matching/member/list")
    public Wrapper<PageData<DemandMemberResponse>> systemMatchingMemberList(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.systemMatchingMemberList(request,sysUser);
    }

    /**
     * 采购询价-采购物料-分页查询
     * @author wrc
     * @date 2020/8/3
     */
    @GetMapping("/productlist/list")
    public Wrapper<PageData<PurchaseInquiryProductRequest>> purchaseInquiryProductlist(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryProductlist(request,sysUser);
    }

    /**
     *查询导入批次号列表
     */
    @GetMapping("/import/batch/list")
    public Wrapper<List<ImportBatchNo>> importBatchList(ImportBatchListRequest request) {
        return purchaseInquiryService.importBatchList(request);
    }

    /**
     *采购需求单导入
     */
    @PostMapping("/import/excel")
    public Wrapper<List<PurchaseInquiryExcelRequest>> importExcel(@RequestParam(value = "file")MultipartFile file) {
        return purchaseInquiryService.importExcel(file);
    }

    /**
     *采购需求单导入检查
     */
    @PostMapping("/import/excel/check")
    public Wrapper<String> importExcelCheck(@RequestParam(value = "file")MultipartFile file) {
        return purchaseInquiryService.importExcelCheck(file);
    }

    /**
     *批量执行报价截止时间到期的需求单外部流程-内部接口
     */
    @PostMapping("/examine/quoted/price/batch")
    public Wrapper examineQuotedPriceBatch(@Validated  @RequestBody ExamineQuotedPriceBatchRequest request) {
         purchaseInquiryService.examineQuotedPriceBatch(request);
         return Wrapper.success();
    }

    /**
     *作废需求单
     */
    @PostMapping("/cancel")
    public Wrapper purchaseInquiryCancel(@Validated  @RequestBody PurchaseInquiryCancelRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.purchaseInquiryCancel(request,sysUser);
    }

    /**
     *需求单详情-根据需求单ID和当前用户ID查询报价单物料详情
     */
    @GetMapping("/getMaterielsByPurchaseInquiryId")
    public Wrapper<PageData<QuotedPriceProductResponse>> getMaterielsByPurchaseInquiryId(@Valid MaterielsByPurchaseInquiryIdRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return purchaseInquiryService.getMaterielsByPurchaseInquiryId(request,sysUser);
    }

    /**
     *采购商机筛选-采购询价列表-需求对接为发布至平台且发布的商城为企业商城且商城环境为WEB的采购需求单
     */
    @GetMapping("/platform/purchase/list")
    public Wrapper<PageData<PurchaseListResponse>> platformPurchaseList(PurchaseListRequest request ) {
        return purchaseInquiryService.platformPurchaseList(request);
    }

    /**
     * 采购公示
     */
    @GetMapping("/purchase/make/list")
    public Wrapper<PageData<PurchaseMakeList>> purchaseMakeList(PurchaseMakeListRequest request){
        return purchaseInquiryService.purchaseMakeList(request);
    }

    /**
     * 商城能力 - 采购商城 - 采购询价单列表
     * @param request 查询参数
     * @return
     */
    @GetMapping("/search/source/list")
    public Wrapper<PageData<SearchReSourceListResponse>> searchReSourceList(PurchaseShopListRequest request) {
        Long shopId = getHeadersShopId();
        return purchaseInquiryService.searchReSourceList(request, isLogin() ? getSysUser() : null,shopId);
    }

    /**
     *商城能力--企业直采--采购门户--询价单详情-接口
     */
    @GetMapping("/search/source/details")
    public Wrapper<PurchaseInquiryDetailsResponse> searchReSourceDetails(BaseDetailsRequest request) {
        return purchaseInquiryService.searchReSourceDetails(request, isLogin() ? getSysUser() : null);
    }

    /**
     * 获取采购需求单内部流程状态
     */
    @GetMapping("/interior/status/purchase")
    public Wrapper<List<BiddingExternalWorkStateEnumResponse>> interiorStatus() {
        return purchaseInquiryService.interiorStatus();
    }

    /**
     * 获取采购需求单外部流程状态
     */
    @GetMapping("/external/status/purchase")
    public Wrapper<List<BiddingExternalWorkStateEnumResponse>> externalStatus() {
        return purchaseInquiryService.externalStatus();
    }

    /**
     * 采购询价--内部流转记录--操作日志
     * @param purchaseInquiryId 询价单ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @RequestMapping(value = "/getInquiryInteriorRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<InteriorPurchaseInquiryLogResponse>> getInquiryInteriorRecordList(PageVO pageVO, @RequestParam(value = "purchaseInquiryId", required = false) Long purchaseInquiryId, @RequestParam(value = "startTime", required = false) Long startTime, @RequestParam(value = "endTime", required = false) Long endTime) {
        Page<InteriorPurchaseInquiryLog> result = purchaseInquiryService.getInquiryInteriorRecordList(pageVO, purchaseInquiryId, startTime, endTime);
        List<InteriorPurchaseInquiryLogResponse> tenderOutCheckRecordList = result.getContent().stream().map(tenderOutCheckRecord -> BeanUtil.copyProperties(tenderOutCheckRecord, InteriorPurchaseInquiryLogResponse.class)).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), tenderOutCheckRecordList));
    }

    /**
     * 采购询价--外部流转记录--操作日志
     * @param purchaseInquiryId 询价单ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @RequestMapping(value = "/getInquiryExternalRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<ExternalPurchaseInquiryLogResponse>> getInquiryExternalRecordList(PageVO pageVO, @RequestParam(value = "purchaseInquiryId", required = false) Long purchaseInquiryId, @RequestParam(value = "startTime", required = false) Long startTime, @RequestParam(value = "endTime", required = false) Long endTime) {
        Page<ExternalPurchaseInquiryLog> result = purchaseInquiryService.getInquiryExternalRecordList(pageVO, purchaseInquiryId, startTime, endTime);
        List<ExternalPurchaseInquiryLogResponse> tenderOutCheckRecordList = result.getContent().stream().map(tenderOutCheckRecord -> BeanUtil.copyProperties(tenderOutCheckRecord, ExternalPurchaseInquiryLogResponse.class)).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), tenderOutCheckRecordList));
    }
}
